package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.Problem;
import dao.ProblemDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/desc")
public class DescServlet extends HttpServlet {
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setStatus(200);
        resp.setContentType("application/json;charset=utf8");

        String idString = req.getParameter("id");
        if(idString == null || "".equals(idString)){
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("<h3>idString丢失!</h3>");
            return;
        }
        int id = Integer.parseInt(idString);
        ProblemDao problemDao = new ProblemDao();
        Problem problem = problemDao.selectOne(id);
        String respString = objectMapper.writeValueAsString(problem);
        resp.getWriter().write(respString);
    }
}
